<?php

namespace app\api\controller;

use app\api\extend\Common;
use think\facade\Db;

class HomeRequicomment extends Common
{
    public function add() {
        $res = $this->check_params(true);
        if (!empty($res)) return $res;

        $data = array_merge($this->params,['from_user_id'=>$this->request->auth['userId'],'create_time'=>date('Y-m-d H:i:s')]);
        $res = $this->model->add($data);
        if (!$res){
            return $this->return_msg(400,'添加需求失败');
        }
        return $this->return_msg(200,'添加需求成功');
    }

    public function list() {
        $res = Db::name('home_requicomment')
        // ->alias('a')
        // ->join('home_muser b','a.muser_id = b.id')
        ->where('status',1)
        // ->field('a.name as title,a.content,a.pic_image,a.price,a.service_time,a.service_duration,a.serve_com,a.status,b.name,b.age,b.avatar_image,b.habitability,b.release_req,b.good_reput,b.user_type,b.exchan_coin,b.mark')
        ->select();
        if (empty($res)) {
             return $this->return_msg(400,'获取失败');
        }
        
        return $this->return_msg(200,'获取成功',$res->toArray());
    }

    public function del()
    {
         $res = $this->check_params(true);
        if (!empty($res)) return $res;
        
        $res = $this->model->del($this->params['id']);
        if (!$res){
            return $this->return_msg(400,'删除失败');
        }
        return $this->return_msg(200,'删除成功',['count'=>$res],false);
    }

    public function update()
    {
        $res = $this->check_params(true);
        if (!empty($res)) return $res;

        $newData = [
            'comment_id'=>$this->params['comment_id'],
        ];
        $newData['data'] = array_diff_key($this->params,array_keys($newData));
        $res = $this->model->upd($newData);
        if (!$res){
            return $this->return_msg(400,'修改失败');
        }
        return $this->return_msg(200,'修改成功',['count'=>$res],false);
    }

}